const express = require("express");
const { multer } = require("../../model/tools");
const settingModel = require("../../model/settingModel");
var router = express.Router();

router.get("/", async (req, res) => {
    var result = await settingModel.find({})
    res.render("admin/setting/index.html", {
        list: result[0]
    })
})


//上传多个图片
var cpUpload = multer().fields([{name:'site_logo',maxCount:1},{name:'no_picture',maxCount:1}])
router.post("/doEdit",cpUpload, async (req, res) => {

    let json ={}
    if(req.files.site_logo){
        var site_logo = req.files.site_logo[0].path.split("\\").slice(1).join("/");
        json=Object.assign(json,{site_logo})

    }
    if(req.files.no_picture){
        var no_picture = req.files.no_picture[0].path.split("\\").slice(1).join("/");
        json=Object.assign(json,{no_picture})
    }
    await settingModel.updateMany({},Object.assign(json,req.body))
    res.render("admin/public/success.html",{
        redirectUrl:`/${req.app.locals.adminPath}/setting`,
        message:"修改数据成功"
    });

})


module.exports = router